# -*- coding: utf-8 -*-
from django.views.generic import TemplateView, ListView, DetailView
from .models import Species, Subspecies , Bird
from apps.home.models import UserProfile
from django.http import HttpResponse , HttpResponseRedirect
from django.shortcuts import render_to_response , render

#from apps.cart.models import Cart
from apps.cart.cart import Cart


class catalog(ListView):
    template_name = 'catalog/catalog_base.html'
    context_object_name = 'species'
    model = Species
    
class subspecies(TemplateView):
    template_name = 'catalog/subspecies.html'
     
    def get_context_data(self, **kwargs):
        context = super(subspecies, self).get_context_data(**kwargs)
        specieTemp = Species.objects.get(id=self.kwargs.get('id'))
        subspeciesTemp = Subspecies.objects.filter(species=self.kwargs.get('id'))
        #context['id'] = self.kwargs.get('id')
        if not subspeciesTemp:
            self.template_name = 'catalog/birds.html'
        else:
            self.template_name = 'catalog/subspecies.html'    
        context['Specie'] = specieTemp
        context['Subspecies'] = subspeciesTemp
        context['Birds'] = Bird.objects.filter(specie=self.kwargs.get('id'))
        return context
    
    
class show_birds(TemplateView):
    template_name = 'catalog/birds.html'
     
    def get_context_data(self, **kwargs):
        context = super(show_birds, self).get_context_data(**kwargs)
        specieTemp = Species.objects.get(id=self.kwargs.get('id'))
        subspecieTemp = Subspecies.objects.get(id=self.kwargs.get('idSub'))
           
        context['Specie'] = specieTemp
        context['Subspecie'] = subspecieTemp
        context['Birds'] = Bird.objects.filter(specie=self.kwargs.get('id'), subspecie=self.kwargs.get('idSub'))
        return context   

      
def go_cart(request):
    cart = Cart(request)
    total = cart.summary()
    return render(request, 'catalog/cart.html', { 'cart':Cart(request),'Total':total, })
 

 
class bird_detail(TemplateView):
    template_name = 'catalog/birdDetail.html'
     
    def get_context_data(self, **kwargs):
        context = super(bird_detail, self).get_context_data(**kwargs)
        #specieTemp = Species.objects.get(id=self.kwargs.get('id'))
        #subspecieTemp = Subspecies.objects.get(id=self.kwargs.get('idSub'))
        birdTemp = Bird.objects.get(id=self.kwargs.get('id'))        
        
        context['Specie'] = birdTemp.specie
        context['Subspecie'] =  -1
        if birdTemp.subspecie:
            context['Subspecie'] =  birdTemp.subspecie
        context['bird'] = birdTemp         

#         if not subspecieTemp:       
#             context['DetailBirds'] = Bird.objects.filter(specie=self.kwargs.get('id'), id=self.kwargs.get('idBir'))
#         else:
#             context['DetailBirds'] = Bird.objects.filter(specie=self.kwargs.get('id'), subspecie=self.kwargs.get('idSub'), id=self.kwargs.get('idBir'))
#      
        return context   
        
      
#*****************************CART**************************************++
class add_to_cart1(TemplateView):
    template_name = 'catalog/birdDetail.html'
     
    def get_context_data(self, **kwargs):
        context = super(bird_detail, self).get_context_data(**kwargs)
        birdTemp = Bird.objects.get(id=self.kwargs.get('id'))    
        quantity = self.kwargs.get('quantity')
        add_to_cart(self.request, birdTemp.id, quantity)
        context['Specie'] = birdTemp.specie
        context['Subspecie'] =  -1
        if birdTemp.subspecie:
            context['Subspecie'] =  birdTemp.subspecie
        context['bird'] = birdTemp         


def add_to_cart(request, pid, quantity):
    product = Bird.objects.get(id=pid)
    cart = Cart(request)
    cart.add(product, product.price, quantity)
    total = cart.summary()
    return render(request, 'catalog/cart.html', { 'cart':Cart(request),'Total':total, })
    

def remove_from_cart(request, pid):
    product = Bird.objects.get(id=pid)
    cart = Cart(request)
    cart.remove(product)
    return render(request,'catalog/cart.html', dict(cart=Cart(request)))
         

def remove_all_to_cart(request):
    cart = Cart(request)
    cart.clear()
    return render(request,'catalog/cart.html', dict(cart=Cart(request)))

def registroCompra(request, pid=0):
    UserP = UserProfile.objects.get(user=pid)
    ppid = pid
    cart = Cart(request)
    total = cart.summary()
    return render(request, 'catalog/registroCompra.html', { 'cart':Cart(request),'Total':total, 'UserP':UserP, 'ppid': ppid,})
